// 定义一个初始化下拉菜单的公共js
layui.use(['form','layer'],function () {
    var $ = layui.$
        ,form = layui.form
        ,layer = layui.layer;

    $.fn.initSelect = function(data,valuePrototype,textPrototype,checkValue,idname){
        $(this).empty();
        $(data).each(function() {
            var o = document.createElement("option");
            o.value = this[valuePrototype];
            o.text = this[textPrototype];
            if (o.value == checkValue) {
                o.selected = 'selected';
            }
            //$("#addRoles")[0].options.add(o);
            $('#' + idname + '')[0].options.add(o);
            form.render();
        })
    }
    // 定义一个通用的ajax回调
    window.ajax = function (url,param,type,contentType) {
        return $.ajax({
            url: url,
            data: param || {},
            type: type || 'GET',
            contentType : contentType || 'application/x-www-form-urlencoded'
        });
    }
    window.success = function (msg,time) {
        layer.msg(msg, {
            offset: 't'
            , icon: 6
            ,time: time == null ? 2000 : 1000
        }, function () {
            var index = parent.layer.getFrameIndex(window.name);
            //关闭当前frame
            parent.layer.close(index);
            location.reload();
        });
    }
    window.failorerror = function (msg,time) {
        layer.msg(msg, {
            offset: 't'
            , icon: 5
            ,time: time == null ? 2000 : 1000
        }, function () {
            var index = parent.layer.getFrameIndex(window.name);
            //关闭当前frame
            parent.layer.close(index);
            location.reload();
        });
    }
})
